package com.pengtu.gsj.controller.zxgc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ibm.icu.util.Calendar;
import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.app.User;
import com.pengtu.gsj.entity.zxgc.ProjectData;
import com.pengtu.gsj.entity.zxgc.SpecialProject;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.system.OrganizationService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.zxgc.ProjectDataService;
import com.pengtu.gsj.service.zxgc.SpecialProjectService;
import com.pengtu.gsj.utils.UserUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("specialProject")
public class SpecialProjectController extends DefaultBaseController {
	@Autowired
	private SpecialProjectService specialProjectService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private ProjectDataService projectDataService;
	@Autowired
	private OrganizationService organizationService;
 
	@ModelAttribute
	public void load(String id, String dataId, Model model){
		SpecialProject specialProject;
		ProjectData projectData;
		if (StringUtils.isNotEmpty(id)) {
			specialProject = specialProjectService.get(id);
		} else {
			specialProject = new SpecialProject();
		}
		if (StringUtils.isNotEmpty(dataId)) {
			projectData = projectDataService.getProjectData(dataId);
		} else {
			projectData = new ProjectData();
		}
		
		model.addAttribute("specialProject", specialProject);
		model.addAttribute("projectData", projectData);
	}
	
	/**
	 * 
	 * listInfo:
	 * 适用: 跳转到专项工程列表页面
	 * @param  model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("listInfo")
	public String listInfo(Model model) {
		if (StringUtils.isNotEmpty(SpringMvcUtils.getParameter("projectName"))) {
			model.addAttribute("filter_LIKES_projectName", SpringMvcUtils.getParameter("projectName"));
		}
		return "zxgc/specialProject_list";
	}
	
	/**
	 * 
	 * list:
	 * 适用:得到专项工程数据 根据状态过滤
	 * @param page
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody 
	@RequestMapping("list")
	public Page<SpecialProject> list(Page<SpecialProject> page, HttpServletRequest request) {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("endTime,status");
			page.setOrder("asc,asc");
		}
		page = specialProjectService.getPage(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * specicalProjectInput:
	 * 适用:
	 * @param specialProject
	 * @param attributes
	 * @param model
	 * @return  新增/修改页面
	 * @exception 
	 * @since  1.0.0
	 */
	@Token(save = true)
	@RequestMapping("input")
	public String specicalProjectInput(@ModelAttribute SpecialProject specialProject, RedirectAttributes attributes, Model model){
		return "zxgc/sepcicalProjectInput";
	}
	
	/**
	 * 
	 * specicalProjectView:
	 * 适用:查看专项工程信息
	 * @param specialProject
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("view")
	public String specicalProjectView(@ModelAttribute SpecialProject specialProject){
		return "zxgc/sepcicalProjectView";
	}
	
	
	@Token(save = true)
	@RequestMapping("editor")
	public String specicalProjectEditor(@ModelAttribute SpecialProject specialProject, Model model){
		//将长度;面积;宽度这样的专项数据拆分转换成list集合
		List<String> list = Arrays.asList(specialProject.getDataTab().split(";"));
		//判断角色  如果是辖区局角色  将数据库中已有是数据返回到页面显示
		//获取当前用户的单位
		User user = UserUtils.getUser();
		//根据用户orgId 与专项工程id 查询得到数据
		ProjectData projectData = new ProjectData();
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("orgId", user.getOrg().getId(), MatchType.EQ);
		queryFilter.addFilter("specialProject.id", specialProject.getId(), MatchType.EQ);
		List<ProjectData> projectDatas = projectDataService.getProjectDatas(queryFilter);
		if (projectDatas.size() > 0) {
			projectData = projectDatas.get(0);
		} 
		List<Attachment> attachments = new ArrayList<Attachment>();
		queryFilter = new QueryFilter();
		queryFilter.addFilter("tableId", user.getOrg().getId(), MatchType.EQ);
		queryFilter.addFilter("partId", specialProject.getId(), MatchType.EQ);
		attachments = systemManageService.getAttachmentList(queryFilter);
		//得到当前单位下专项工程的附件信息
		model.addAttribute("list", list);
		model.addAttribute("attachments", attachments);
		model.addAttribute("projectData", projectData);
		model.addAttribute("orgId", user.getOrg().getId());
		return "zxgc/sepcicalProjectEditor";
	}
	
	/**
	 * 
	 * saveCode:
	 * 适用:保存对象
	 * @param code 对象
	 * @param request 请求
	 * @param model 传递参数
	 * @return 码表列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@Token(remove = true)
	@RequestMapping(value = "save",method=RequestMethod.POST)
	public SpecialProject saveProject(@ModelAttribute SpecialProject specialProject,RedirectAttributes attributes,Model model) {
		if(StringUtils.isEmpty(specialProject.getId())){
			//设置工程年份信息
			specialProject.setYear(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		}
		specialProjectService.save(specialProject);
		return specialProject;
	}
	
	@Token(remove = true)
	@RequestMapping(value = "saveData",method=RequestMethod.POST)
	public String saveProjectData(@ModelAttribute ProjectData projectData,RedirectAttributes attributes,Model model) {
		String projId = SpringMvcUtils.getParameter("projId");
		if (StringUtils.isNotEmpty(projId)) {
			SpecialProject specialProject = specialProjectService.get(projId);
			if (specialProject != null) {
				projectData.setSpecialProject(specialProject);
			}
		}
		projectDataService.saveProjectData(projectData);
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		attributes.addAttribute("LId", SpringMvcUtils.getParameter("LId"));
		return "redirect:/specialProject/listInfo.do";
	}
	
	/**
	 * 
	 * releaseProject:
	 * 适用:ajax请求 将项目更新为作废状态
	 * @param specialProject
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("release")
	public boolean releaseProject(@ModelAttribute SpecialProject specialProject)throws Exception{
		specialProject.setStatus("1"); //设置作废状态 
		specialProjectService.save(specialProject);
		return true;
	}
	
	/**
	 * 
	 * closeProject:
	 * 适用:ajax请求 将项目更新为已关闭状态
	 * @param specialProject
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("close")
	public boolean closeProject(@ModelAttribute SpecialProject specialProject)throws Exception{
		specialProject.setStatus("2"); //设置已关闭 
		specialProjectService.save(specialProject);
		return true;
	}
	
	@ResponseBody
	@RequestMapping("delete")
	public boolean deleteProject(@ModelAttribute SpecialProject specialProject) {
		specialProjectService.delete(specialProject);
		return true;
	}
	
	/**
	 * 
	 * upload:
	 * 适用:上传页面传过来的文件
	 * @param multipartFile
	 * @param request
	 * @param attachment
	 * @param attributes
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public boolean upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile, HttpServletRequest request, @ModelAttribute Attachment attachment, RedirectAttributes attributes) throws Exception{
		String projectId = ((MultipartHttpServletRequest)request).getParameter("projectId");
		String tableName = ((MultipartHttpServletRequest)request).getParameter("tableName");
		String tableField = ((MultipartHttpServletRequest)request).getParameter("tableField");
		attachment.setPartId(projectId);
		attachment.setTableName(tableName);
		attachment.setTableField(tableField);
		systemManageService.uploadFile(multipartFile, attachment);
		return true;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "org", method = RequestMethod.POST)
	public Map<String, Object> getOrgAndFile(String projectId) {
		Map<String, Object> map = new HashMap<String, Object>();
		//专项工程文件
		map = systemManageService.getOrgAndFile(projectId);
		//专项数据
		specialProjectService.getStaticData(projectId, map);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "loadFiles", method = RequestMethod.POST)
	public Map<String, Object> getProjectFiles(String projectId) {
		//专项工程文件
		return systemManageService.getOrgAndFile(projectId);
	}
	
	
	@RequestMapping("statistics")
	public String statisticsData(Model model){
		return "tjfx/zxgc_statistics";
	}
	
	@ResponseBody
	@RequestMapping(value = "countJson", method=RequestMethod.POST)
	public List<Map<String, Object>> getCountJson() {
		return specialProjectService.getCountJson();
	}
	
	@ResponseBody
	@RequestMapping("export")
	public ResponseEntity<byte[]> exportData() throws Exception {
		return specialProjectService.exportData();
	}
	
}
